package com.atguigu.gulimall.cart.contorller;

import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.gulimall.cart.interceptor.CartInterceptor;
import com.atguigu.gulimall.cart.service.CartService;
import com.atguigu.gulimall.cart.vo.Cart;
import com.atguigu.gulimall.cart.vo.CartItem;
import com.atguigu.gulimall.cart.vo.UserInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {


    @Autowired
    CartService cartService;

    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItem> getCurrentUserCartItems(){
        //不用传用户的ID，
        // 拦截器已经从 session 中拦截到了
        List<CartItem> userCartItems =  cartService.getUserCartItems();
        return userCartItems;
    }

    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){

        cartService.deletItem(skuId);

        return "redirect:http://cart.gulimall.com/cart.html";  //再查一遍购物车

    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num){

        cartService.changeItemCount(skuId,num);

        return "redirect:http://cart.gulimall.com/cart.html";  //再查一遍购物车

    }



    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("check") Integer check){

        cartService.checkItem(skuId,check);

        return "redirect:http://cart.gulimall.com/cart.html";  //再查一遍购物车
    }

    //获取购物车的商品列表
    //去购物车,执行之前经过了CartInterceptor
    @GetMapping("cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        //已经拦截过了，获取拦截信息,拿到CartInterceptor中所有线程共享的静态变量，不过只拿自己线程对应的那一份

        Cart cart = cartService.getCart();
        model.addAttribute("cart",cart);

        return "cartList";
    }

    //addFlashAtrributes 放到session里，只能用一次
    //addAtrributes 跳转的时候自动拼上去要加的属性
    //添加商品
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes ra) throws ExecutionException, InterruptedException {

        //拿到spuId，和num， 封装为CartItem；加入redis

        //跳转再查一次，刷新就不会重复提交

        CartItem cartItem = cartService.addToCart(skuId,num);
        ra.addAttribute("skuId",skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,Model model){

        //再查一次
        CartItem item = cartService.getCartItem(skuId);

        model.addAttribute("item",item);

        return "success";

    }



}
